{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-08T07:33:38.325790Z",
     "start_time": "2025-05-08T07:33:38.028716Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#线性核\n",
    "import numpy as np\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.datasets import make_classification\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# 生成一些线性可分的数据\n",
    "X, y = make_classification(n_samples=100, n_features=2, n_redundant=0, n_informative=2, random_state=5, n_clusters_per_class=1)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
    "\n",
    "# 使用线性核的SVM\n",
    "linear_svc = SVC(kernel='linear')\n",
    "linear_svc.fit(X_train, y_train)\n",
    "y_pred = linear_svc.predict(X_test)\n",
    "print(f\"Linear Kernel Accuracy: {accuracy_score(y_test, y_pred)}\")\n"
   ],
   "id": "c104dcb3b53b0bb5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linear Kernel Accuracy: 0.9666666666666667\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-08T07:33:38.343413Z",
     "start_time": "2025-05-08T07:33:38.335795Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#多项式核\n",
    "# 使用多项式核的SVM\n",
    "poly_svc = SVC(kernel='poly', degree=3)\n",
    "poly_svc.fit(X_train, y_train)\n",
    "y_pred = poly_svc.predict(X_test)\n",
    "print(f\"Polynomial Kernel Accuracy: {accuracy_score(y_test, y_pred)}\")\n"
   ],
   "id": "530e5f7e4597f5ab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Polynomial Kernel Accuracy: 0.9333333333333333\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-08T07:33:38.372032Z",
     "start_time": "2025-05-08T07:33:38.366420Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#高斯核（RBF核）\n",
    "# 使用高斯核（RBF核）的SVM\n",
    "rbf_svc = SVC(kernel='rbf', gamma='scale')\n",
    "rbf_svc.fit(X_train, y_train)\n",
    "y_pred = rbf_svc.predict(X_test)\n",
    "print(f\"Gaussian (RBF) Kernel Accuracy: {accuracy_score(y_test, y_pred)}\")\n"
   ],
   "id": "519e95ee52893bd5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gaussian (RBF) Kernel Accuracy: 0.9666666666666667\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-08T07:33:38.451566Z",
     "start_time": "2025-05-08T07:33:38.444770Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#Sigmoid核\n",
    "# 使用Sigmoid核的SVM\n",
    "sigmoid_svc = SVC(kernel='sigmoid')\n",
    "sigmoid_svc.fit(X_train, y_train)\n",
    "y_pred = sigmoid_svc.predict(X_test)\n",
    "print(f\"Sigmoid Kernel Accuracy: {accuracy_score(y_test, y_pred)}\")\n"
   ],
   "id": "a7461983b3a0d746",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sigmoid Kernel Accuracy: 0.8666666666666667\n"
     ]
    }
   ],
   "execution_count": 19
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
